package org.jeecg.utils;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import java.util.HashMap;


public class OcrApi {
    private static final String APP_ID = "116769874";
    private static final String API_KEY = "Q1Matc3p9KH7W9IgL6GQwgKp";
    private static final String SECRET_KEY = "QHZkpX8NJ3zOr6C6casP46BXWYIVcebR";

    private static AipOcr getAipClient() {
        return getAipClient(API_KEY, SECRET_KEY);
    }

    public static AipOcr getAipClient(String apiKey, String secretKey) {
        AipOcr client = new AipOcr(APP_ID, apiKey, secretKey);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        return client;
    }

    public static String result(AipOcr client) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");

        JSONObject res = client.basicGeneralUrl(
                "图片路径", options);
        return res.toString(2);
    }

    public static void main(String[] args) {
        System.out.println(result(getAipClient()));
    }
}